package com.demo.blog;

import cn.techjc.SSHService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;
import com.jfinal.plugin.activerecord.tx.Tx;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * BlogController
 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
@Before(BlogInterceptor.class)
public class ApiController extends Controller {
	private static final SSHService sshService = new SSHService();

	public void index() {
		int remotePort = getParaToInt("rport");
		String type = getPara("type");
		Map<String,Object> map = new HashMap<>();
		if("on".equals(type)){
			String localIP = getPara("lip");
			int localPort = getParaToInt("lport");
			map = sshService.startForwardRemote(localIP,localPort,remotePort);
		}else if("off".equals(type)){
			map = sshService.shutdownForwardRemote(remotePort);
		}
		renderJson(map);
	}

	public void list() {
		setAttr("code","1");
		setAttr("message","查询成功");
		setAttr("data",sshService.getCache());
		renderJson();
	}

	public void reset() {
		renderJson(sshService.resetConnection());
	}

	public void getIp() {
		HttpServletRequest request = getRequest();
		setAttr("code","1");
		setAttr("message","查询成功");
		setAttr("data",request.getRemoteAddr());
		renderJson();
	}
	

}


